const { Readable } = require('stream');
/**
 * 
 * @param {*} stream  流文件
 */
const readBody = async (stream) => {
    if (stream instanceof Readable) {
        let res = '';
        return new Promise((resolve, reject) => {
            stream.on('data', (data) => {
                res += data;
            })
            stream.on('end', () => {
                //解析完成抛出结果
                resolve(res)
            })
        })
    } else {
        return stream.toString()
    }

}


const path = require('path');
/**
 * 导入模块拼接函数
 * @param {app } param0 当前koa实例
 * @param {root } param1 root 对象
 */
function resolveVue(root) {
    const compilerpkgPath = path.join(root, 'node_module', '@vue/compiler-sfc/package.json');

    const compilerpkg = require(compilerpkgPath) //拿到对应的json

    const compilerPath = path.join(path.dirname(compilerpkg), compilerpkg.main);

    const resolvePath = (name) => path.resolve(root, 'node_module', `@vue/${name}/dist/${name}.esm-bundler.js`)

    const runtimeDomPath = resolvePath('runtime-dom')
    const runtimeCorePath = resolvePath('runtime-core')
    const reactivityPath = resolvePath('reactivity')
    const sharedPath = resolvePath('shared')
    //创建映射关系
    return {
        compiler: compilerPath,
        '@vue/runtime-dom': runtimeDomPath,
        '@vue/runtime-core': runtimeCorePath,
        '@vue/reactivity': reactivityPath,
        '@vue/shared': sharedPath,
        vue: runtimeDomPath
    }
}


module.exports = { readBody, resolveVue };